﻿#region Header

/*
    This file is part of nDoctor Light

    NDoctor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    NDoctor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with NDoctor.  If not, see <http://www.gnu.org/licenses/>.
*/

#endregion Header

namespace Probel.Ldoc.ViewModels.Controls
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Input;

    using Probel.Ldoc.Services.Dto;
    using Probel.Ldoc.Services.Services;
    using Probel.Ldoc.ViewModels.Dto;
    using Probel.Ldoc.ViewModels.Helpers;
    using Probel.Ldoc.ViewModels.Pages;
    using Probel.Ldoc.ViewModels.Properties;
    using Probel.Mvvm.DataBinding;

    public class DoctorPopupViewModel : BaseViewModel
    {
        #region Fields

        private readonly ICriterionFactory CriterionFactory;
        private readonly PatientCardDto CurrentPatient;
        private readonly PatientEditorViewModel ParentViewModel;
        private readonly ICommand searchCommand;
        private readonly IPatientService Service;

        private string criterion;
        private DoctorCardDto selectedDoctor;

        #endregion Fields

        #region Constructors

        public DoctorPopupViewModel(PatientEditorViewModel parent
            , IErrorService errorService
            , IPatientService service
            , PatientCardDto patient
            , ICriterionFactory criterionFactory
            , bool isAsync = true)
            : base(errorService, isAsync)
        {
            this.ParentViewModel = parent;
            this.Service = service;
            this.CurrentPatient = patient;
            this.CriterionFactory = criterionFactory;

            this.Doctors = new ObservableCollection<DoctorCardDto>();

            this.searchCommand = new RelayCommand(this.Search, this.CanSearch);
        }

        #endregion Constructors

        #region Properties

        public string Criterion
        {
            get { return this.criterion; }
            set
            {
                this.criterion = value;
                this.OnPropertyChanged(() => Criterion);
            }
        }

        public ObservableCollection<DoctorCardDto> Doctors
        {
            get;
            private set;
        }

        public ICommand SearchCommand
        {
            get { return this.searchCommand; }
        }

        public DoctorCardDto SelectedDoctor
        {
            get { return this.selectedDoctor; }
            set
            {
                this.selectedDoctor = value;
                this.OnPropertyChanged(() => SelectedDoctor);
            }
        }

        #endregion Properties

        #region Methods

        public void BindDoctor()
        {
            if (this.SelectedDoctor != null)
            {
                if (!this.Service.IsBinded(this.SelectedDoctor, this.CurrentPatient))
                {
                    this.Service.Bind(this.SelectedDoctor, this.CurrentPatient);

                    this.ParentViewModel.UiMessage = UiMessageDto.Info(string.Format(Messages.Msg_DoctorBinded
                        , string.Format("{0} {1}", this.SelectedDoctor.FirstName, this.SelectedDoctor.LastName)));

                    this.ParentViewModel.RefreshDoctors();
                }
                else { this.ParentViewModel.UiMessage = UiMessageDto.Warn(Messages.Msg_DoctorAlreadyBinded); }
            }
            else { this.ParentViewModel.UiMessage = UiMessageDto.Warn(Messages.Msg_NoDoctorAdded); }
        }

        private bool CanSearch()
        {
            return !string.IsNullOrEmpty(this.Criterion);
        }

        private void Search()
        {
            this.Async(() => this.Service.GetDoctors(CriterionFactory.Build(this.Criterion))
                      , r => this.Doctors.Refill(r));
        }

        #endregion Methods
    }
}